#!/bin/sh

# 5分钟检测一次
full_check_period=180 # 3mins
LOG_SIZE_MAX=4194304 # by bytes, 4194304B=4MB
TOTAL_LOG_SIZE_MAX=204800 # by KB, 204800KB=200MB
SRC_LOG_PATH=/var/alog/AtlasEdge_log/
DEST_LOG_PATH=/home/log/alog/AtlasEdge_log/
LOG_FILE_PERMISSION_MODE=400 # file permission

# arg1--src_file_path: log file with full path
# arg2--dst_dir: be used to save as for log file
save_log_file()
{
    src_file_path=$1
    dst_dir=$2

    if [ ! -f ${src_file_path} ];then
        return 1
    fi

    if [ ! -d ${dst_dir} ];then
        mkdir -p ${dst_dir}
    fi

    item_file_name=`echo ${src_file_path} |awk -F '/' '{print $NF}'`
    cp ${src_file_path} /tmp/${item_file_name}.1
    cat /dev/null > ${src_file_path}
    chmod ${LOG_FILE_PERMISSION_MODE} /tmp/${item_file_name}.1
    date_curr=`date +%Y%m%d"_"%H%M%S`
    dest_file=${item_file_name}_${date_curr}.tar.gz
    tar -czf /tmp/${dest_file} /tmp/${item_file_name}.1 >/dev/null 2>&1
    chmod ${LOG_FILE_PERMISSION_MODE} /tmp/${dest_file}
    mv /tmp/${dest_file}  $dst_dir/
    rm -rf /tmp/${item_file_name}.1

    return 0
}

# arg1--FILE_MAX_SIZE: The threhold of logfile size for saving as
# arg2--src_dir: log files path
# arg3--dst_dir: log path for saving as
save_log_file_by_size()
{
    local FILE_MAX_SIZE=$1
    local src_dir=$2
    local dst_dir=$3

    if [ ! -d ${src_dir} ];then
        return 0
    fi

    if [ ! -d $dst_dir ];then
        mkdir -p ${dst_dir}
    fi

    filenum=`find ${src_dir}/ -name "*.log"|wc|awk '{print $1}'`
    if [ ${filenum} -eq 0 ];then
        return 0
    fi

    filepathlist=`find ${src_dir} -name "*.log"`
    for itemfilepath in ${filepathlist}
    do
        cur_file_size=`ls -l ${itemfilepath} | awk '{print $5}'`
        cur_file_size=${cur_file_size:=0}
        if [ ${cur_file_size} -gt ${FILE_MAX_SIZE} ];then
            save_log_file ${itemfilepath} ${dst_dir}
        fi
    done
}

# 日志的转存空间达到阈值时，直接删除最老的日志包
check_dst_log_ram_by_size()
{
    local FILES_SIZE_THRESHOLD=$1
    local check_dir=$2

    if [ ! -d ${check_dir} ];then
        return 0
    fi
    
    local 
    while [ 1 ]
    do
        total_size=`du ${check_dir} |awk '{print $1}'`
        total_size=${total_size:=0}
        if [ ${total_size} -gt ${FILES_SIZE_THRESHOLD} ];then
            file_num=`find ${check_dir}/ -name "*.gz"|wc|awk '{print $1}'`
            file_num=${file_num:=0}
            if [ ${file_num} -gt 0 ];then
                file_name_curr=`find ${check_dir}/* -type f |xargs ls -t |tail -n1`
                rm -rf ${file_name_curr}
                echo "rm -rf ${file_name_curr}"
            fi
        else
            break
        fi
    done
}

src_tar_file_save_as()
{
    local src_dir=$1
    local dst_dir=$2

    if [ ! -d ${dst_dir} ];then
        mkdir -p ${dst_dir}
    fi

    local file_list=`find ${src_dir} -name "*.gz"`
    for i in ${file_list}
    do
        mv $i ${dst_dir}
    done
}

log_file_check()
{
    while [ 1 ]
    do
        # 若单个日志文件达到阈值(比如：4M)，则先压缩,后转存
        save_log_file_by_size ${LOG_SIZE_MAX} ${SRC_LOG_PATH} ${DEST_LOG_PATH}

        # 转存tar包日志
        src_tar_file_save_as ${SRC_LOG_PATH} ${DEST_LOG_PATH}

        # 日志的转存空间达到阈值时，删除最旧的日志
        check_dst_log_ram_by_size ${TOTAL_LOG_SIZE_MAX} ${DEST_LOG_PATH}

        sleep ${full_check_period}
    done
}

log_file_check_main()
{
    # check the existed tar file permission
    find ${DEST_LOG_PATH} -name "*.gz" | xargs chmod ${LOG_FILE_PERMISSION_MODE} >/dev/null 2>&1

    # check the log and dumping log files
    log_file_check
}

log_file_check_main
exit 0
